require "common"
local ZXMap = {1,2,3,2,3}
--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )
		if len % 2 ~= 0 then return 0 end

        for i=1,len,2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) < 1 then return nil end

        end
        return true
end

--数字字符串排序

function sort_str_syw( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len,2 do
		table.insert( st, string.sub(num,i,i+1) )
	end
	table.sort(st)
	for i=1,len / 2 do
		out = out..st[i]
	end

	return out

end
--获取相同的号码的个数
function get_comm_num(num1, num2)
	local comm = 0
	local str1 = {}
	local str2 = {}
	for i = 1, string.len(num1), 2 do
		str1[(i+1)/2] = string.sub(num1, i, i+1)
	end
	for i = 1, string.len(num2), 2 do
		str2[(i+1)/2] = string.sub(num2, i, i+1)
	end
	for i = 1, #str1 do
		for j = 1, #str2 do
			if str1[i] == str2[j] then
				comm = comm + 1
			end
		end
	end

	return comm

end
--获取相同的号码的个数
function get_comm_num3(num1, num2, num3)
	local comm = 0
	local str1 = {}
	local str2 = {}
	local str3 = {}
	for i = 1, string.len(num1), 2 do
		str1[(i+1)/2] = string.sub(num1, i, i+1)
	end
	for i = 1, string.len(num2), 2 do
		str2[(i+1)/2] = string.sub(num2, i, i+1)
	end
	for i = 1, string.len(num3), 2 do
		str3[(i+1)/2] = string.sub(num3, i, i+1)
	end
	for i = 1, #str1 do
		for j = 1, #str2 do
			if str1[i] == str2[j] then
				for k = 1, #str3 do
					if str1[i] == str3[k] then
						comm = comm + 1
					end
				end
			end
		end
	end

	return comm

end
--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str_syw(num)
	for i=1,len ,2 do
		c = string.sub(str,i,i+1)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end


function check_xysc( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0
	local distNum = 0
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	xItem = Split(itemList[1], "#")

	--前一 二 三直选 过二三关直选
	if butType == '1' or butType == '2' or butType == '3' or butType == '4' or butType == '5' then
		if #xItem ~= 1 then return 0 end
		len = string.len(xItem[1])
		if len / 2 ~= ZXMap[tonumber(butType)] or len % 2 ~= 0 then return 0 end
		if not judge_str(xItem[1], 12) then return 0 end
		if butType == '1' or butType == '2' or butType == '3' then
			_, distNum = get_distinct_num(xItem[1])
			if distNum ~= len / 2 then return 0 end
		end
		butNum = 1
	--前二三复式
	elseif butType == '6' or butType == '7' then
		if #xItem ~= 1 then return 0 end
		len = string.len(xItem[1])
		if len % 2 ~= 0 then return 0 end
		_, distNum = get_distinct_num(xItem[1])
		if distNum ~= len / 2 then return 0 end
		if not judge_str(xItem[1], 12) then return 0 end
		butType = tonumber(butType) - 4
		if distNum >12 or distNum < butType then return 0 end
		butNum = permutation(distNum, butType)
	--前二复式对位
	elseif butType == '8' then
		if #xItem ~= 2 then return 0 end
		num = {0,0}
		for i = 1, 2 do
			len = string.len(xItem[i])
			if len % 2 ~= 0 then return 0 end
			_, distNum = get_distinct_num(xItem[i])
			if distNum ~= len / 2 then return 0 end
			if not judge_str(xItem[i], 12) then return 0 end
			num[i] = distNum
		end
		if num[1] + num[2] > 24 or num[1] + num[2] < 3 then return 0 end
		commNum = get_comm_num(xItem[1], xItem[2])
		butNum = num[1] * num [2] - commNum
	--前三复式对位
	elseif butType == '9' then
		if #xItem ~= 3 then return 0 end
		num = {0,0,0}
		for i = 1, 3 do
			len = string.len(xItem[i])
			if len % 2 ~= 0 then return 0 end
			_, distNum = get_distinct_num(xItem[i])
			if distNum ~= len / 2 then return 0 end
			if not judge_str(xItem[i], 12) then return 0 end
			num[i] = distNum
		end
		if num[1] + num[2] + num[3] > 36 or num[1] + num[2] + num[3] < 4 then return 0 end
		commNum1 = get_comm_num(xItem[1], xItem[2])
		commNum2 = get_comm_num(xItem[2], xItem[3])
		commNum3 = get_comm_num(xItem[1], xItem[3])
		commNum4 = get_comm_num3(xItem[1], xItem[2], xItem[3])
		butNum = num[1] * num [2] * num[3] - num[3] * commNum1 - num[1] * commNum2 - num[2] * commNum3 + 2* commNum4
		if butNum < 1 then return 0 end
	--过二关过三关对位
	elseif butType == 'a' or butType == 'b' then
		nTotal = 0
		butType = string.byte(butType) - string.byte('a') + 2
		if #xItem ~= butType then return 0 end
		num = {}
		for i = 1, #xItem do
			len = string.len(xItem[i])
			if len % 2 ~= 0 then return 0 end
			_, distNum = get_distinct_num(xItem[i])
			if distNum ~= len / 2 then return 0 end
			if not judge_str(xItem[i], 12) then return 0 end
			num[i] = distNum
		end
		butNum = 1
		for i = 1, #xItem do
			nTotal = nTotal + num[i]
		end
		if nTotal < butType + 1 or nTotal > #xItem * 12 then return 0 end
		for i=1, #xItem do
			butNum = butNum *num[i]
		end
	--前二三组选
	elseif butType == 'c' or butType == 'd' then
		butType =string.byte(butType) - string.byte('a')
		if #xItem ~= 1 then return 0 end
		len = string.len(xItem[1])
		_,distNum = get_distinct_num(xItem[1])
		if distNum ~= len / 2 then return 0 end

		if butType == 2 then
			if len < butType or len > 24 then return 0 end
		end
		if  not judge_str(xItem[1],12) then return 0 end
		butNum = combination(len/2,butType)
	--前二直选胆拖前三直选胆拖                   过二关 过三关胆拖                 组二组三胆拖
	elseif butType == 'e' or butType == 'f' or butType == 'g' or butType == 'h' or butType == 'i' or butType == 'j' then
		butTypeTemp = butType
		if butType == 'e' or butType == 'f' then
			butType = string.byte(butType) - string.byte('c')
		elseif butType == 'g' or butType == 'h' then
			butType = string.byte(butType) - string.byte('e')
		elseif butType == 'i' or butType == 'j' then
			butType = string.byte(butType) - string.byte('g')
		end
		if #xItem ~= 2  or is_str_mix(xItem[1],xItem[2]) then return 0 end
		if   not judge_str(xItem[1],12) or not judge_str(xItem[2],12) then return 0 end
		num = {}
		for i = 1, #xItem do
			len = string.len(xItem[i])
			if len % 2 ~= 0 then return 0 end
			_, distNum = get_distinct_num(xItem[i])
			if distNum ~= len / 2 then return 0 end
			if not judge_str(xItem[i], 12) then return 0 end
			num[i] = distNum
		end

		if num[1] >= butType or num[1] + num[2] < butType + 1 or num[1] + num[2] > 12 then return 0 end
		if butTypeTemp == 'e' or butTypeTemp == 'f' or butTypeTemp == 'g' or butTypeTemp == 'h' then
			butNum = combination(num[2],butType - num[1]) * permutation(butType, butType)
		else
			butNum = combination(num[2],butType - num[1])
		end
	--位置
	elseif butType == 'k' then
		if #xItem ~= 1 then return 0 end
		if string.len(xItem[1]) ~= 2 then return 0 end
		if not judge_str(xItem[1],12) then return 0 end
		butNum = 1
	--【1-4 （数1代表小奇，2小偶，3大奇，4大偶）】大小奇偶
	elseif butType == 'l' then
		if #xItem ~= 1 then return 0 end
		len = string.len(xItem[1])
		if len < 2 or len > 8 then return 0 end
		if not judge_str(xItem[1],4) then return 0 end
		_, distNum = get_distinct_num(xItem[1])
		if distNum ~= len / 2 then return 0 end
		if len % 2 ~= 0 then return 0 end
		butNum = len / 2
	end

	return butNum,butPower,inStr,inStr
end

function query_xysc(butType,queryStr,djStr)
	local bingo = 0
	local bingoNum = {}
	local lotNum = {}
	local row = {0,0,0,0,0}
	local column = {0,0,0,0,0}
	local coorStr = ""
	local nNum = 0
	itemList = Split( queryStr, "#B" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	for i = 1, string.len(djStr), 2 do
		bingoNum[(i+1)/2] = string.sub(djStr, i, i + 1)
	end


	--前一直选
	if butType == '1' then
		if itemList[1] == bingoNum[1] then
			bingo = 1
			row[1] = 1
			column[1] = tonumber(itemList[1])
		end
	elseif butType == '2' or butType == '4' then
		for i = 1, string.len(itemList[1]), 2 do
			lotNum[(i+1)/2] = string.sub(itemList[1], i, i+1)
		end

		if lotNum[1] == bingoNum[1] and lotNum[2] == bingoNum[2] then
			bingo = 1
			row[1] = tonumber(lotNum[1])
			column[1] = tonumber(lotNum[2])

		end
	elseif butType == '3' or butType == '5' then
		for i = 1, string.len(itemList[1]), 2 do
			lotNum[(i+1)/2] = string.sub(itemList[1], i, i+1)
		end
		if itemList[1] == djStr then
			bingo = 1
			row[1] = (tonumber(lotNum[1])-1)*12 + tonumber(lotNum[2])
			column[1] = tonumber(lotNum[3])
		end
	elseif butType == '6' then
		nNum = ConputeBingoNum(itemList[1] ,string.sub(djStr, 1, 4) ,2)
		if nNum == 2 then
			bingo = 1
			row[1] = tonumber(bingoNum[1])
			column[1] = tonumber(bingoNum[2])
		end
	elseif butType == '7' then
		nNum = ConputeBingoNum(itemList[1] ,djStr, 2)
		if nNum == 3 then
			bingo = 1
			row[1] = (tonumber(bingoNum[1])-1)*12 + tonumber(bingoNum[2])
			column[1] = tonumber(bingoNum[3])
		end
	elseif butType == '8' or butType == 'a' then
		xItem = Split(itemList[1], "#")
		if ConputeBingoNum(xItem[1], bingoNum[1], 2) == 1 and ConputeBingoNum(xItem[2], bingoNum[2], 2) == 1 then
			bingo = 1
			row[1] = tonumber(bingoNum[1])
			column[1] = tonumber(bingoNum[2])
		end
	elseif butType == '9' or butType == 'b' then
		xItem = Split(itemList[1], "#")
		if ConputeBingoNum(xItem[1], bingoNum[1], 2) == 1 and ConputeBingoNum(xItem[2], bingoNum[2], 2) == 1 and ConputeBingoNum(xItem[3], bingoNum[3], 2) == 1 then
			bingo = 1
			row[1] = (tonumber(bingoNum[1])-1)*12 + tonumber(bingoNum[2])
			column[1] = tonumber(bingoNum[3])
		end
	elseif butType == 'c' then
		nNum = ConputeBingoNum(itemList[1], string.sub(djStr, 1, 4), 2)
		if nNum == 2 then
			bingoNum[3] = '13'
			table.sort(bingoNum)
			bingo = 1
			row[1] = tonumber(bingoNum[1])
			column[1] = tonumber(bingoNum[2])
		end
	elseif butType == 'd' then
		nNum = ConputeBingoNum(itemList[1], djStr, 2)
		if nNum == 3 then
			table.sort(bingoNum)
			bingo = 1
			row[1] = (tonumber(bingoNum[1])-1)*12 + tonumber(bingoNum[2])
			column[1] = tonumber(bingoNum[3])
		end
	elseif butType == 'e' or butType == 'g' then
		xItem = Split(itemList[1], "#")
		if ConputeBingoNum(xItem[1], string.sub(djStr, 1, 4), 2) == 1 and ConputeBingoNum(xItem[2], string.sub(djStr, 1, 4), 2) == 1 then
			bingo = 1
			row[1] = tonumber(bingoNum[1])
			column[1] = tonumber(bingoNum[2])
		end
	elseif butType == 'f' or butType == 'h' then
		xItem = Split(itemList[1], "#")
		bingoD = ConputeBingoNum(xItem[1], djStr, 2)
		if bingoD == string.len(xItem[1]) / 2 then
			bingoT = ConputeBingoNum(xItem[2], djStr, 2)
			if bingoD + bingoT == 3 then
				bingo = 1
				row[1] = (tonumber(bingoNum[1])-1)*12 + tonumber(bingoNum[2])
				column[1] = tonumber(bingoNum[3])
			end
		end
	elseif butType == 'i' then
		xItem = Split(itemList[1], "#")
		if ConputeBingoNum(xItem[1], string.sub(djStr, 1, 4), 2) == 1 and ConputeBingoNum(xItem[2], string.sub(djStr, 1, 4), 2) == 1 then
			bingoNum[3] = '13'
			table.sort(bingoNum)
			bingo = 1
			row[1] = tonumber(bingoNum[1])
			column[1] = tonumber(bingoNum[2])
		end
	elseif butType == 'j' then
		xItem = Split(itemList[1], "#")
		bingoD = ConputeBingoNum(xItem[1], djStr, 2)
		if bingoD == string.len(xItem[1]) / 2 then
			bingoT = ConputeBingoNum(xItem[2], djStr, 2)
			if bingoD + bingoT == 3 then
				table.sort(bingoNum)
				bingo = 1
				row[1] = (tonumber(bingoNum[1])-1)*12 + tonumber(bingoNum[2])
				column[1] = tonumber(bingoNum[3])
			end
		end
	elseif butType == 'k' then
		if ConputeBingoNum(itemList[1], djStr, 2) == 1 then
			bingo = 1
			row[1] = 1
			column[1] = tonumber(itemList[1])
		end
	elseif butType == 'l' then
		for i = 1, string.len(itemList[1]), 2 do
			lotNum[(i+1)/2] = string.sub(itemList[1], i, i+1)
		end

		for i=1, #lotNum do
			if lotNum[i] == "01" then
				if bingoNum[1] == "01" or bingoNum[1] == "03" or bingoNum[1] == "05" then
					bingo = 1
					row[1] = 1
					column[1] = tonumber(lotNum[i])
					break
				end
			elseif lotNum[i] == "02" then
				if bingoNum[1] == "02" or bingoNum[1] == "04" or bingoNum[1] == "06" then
					bingo = 1
					row[1] = 1
					column[1] = tonumber(lotNum[i])
					break
				end
			elseif lotNum[i] == "03" then
				if bingoNum[1] == "07" or bingoNum[1] == "09" or bingoNum[1] == "11" then
					bingo = 1
					row[1] = 1
					column[1] = tonumber(lotNum[i])
					break
				end
			elseif lotNum[i] == "04" then
				if bingoNum[1] == "08" or bingoNum[1] == "10" or bingoNum[1] == "12" then
					bingo = 1
					row[1] = 1
					column[1] = tonumber(lotNum[i])
					break
				end
			end
		end
	else
		return 0
	end
	--坐标

	coorStr = coorStr..row[1]..","..column[1]
	bingo = bingo * butPower

	return bingo.."-"..coorStr

end


--print(check_xysc('f',"05#060708"))
--print(check_xysc('l',"010203#B10"))
--print(query_xysc('c', "03060811#B1", "080604"))
